package cn.zgf.core;

import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author GaoFeng2017
 * @date 2018-02-21 14:48:59
 **/

public class WriteExcel {
    private String filePath;
    private XSSFWorkbook workbook;
    private XSSFSheet sheet;
    private List content;

    public WriteExcel(String filePath, String sheetName, List content) {
        this.filePath = filePath;
        this.workbook = new XSSFWorkbook();
        this.sheet = workbook.createSheet(sheetName);
        this.content = content;
    }

    public void write() {
        List<String> heads = this.getHeads();
        XSSFRow row = sheet.createRow(0);
        /*创建标题样式*/
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        XSSFFont xssfFont = workbook.createFont();
        xssfFont.setBold(true);
        xssfFont.setFontName("宋体");
        xssfFont.setFontHeightInPoints((short) 10);
        cellStyle.setFont(xssfFont);

        /*创建标题文本*/
        for (int j = 0;j < heads.size();j++) {
            XSSFCell xssfCell = row.createCell(j);
            xssfCell.setCellValue(heads.get(j));
            xssfCell.setCellStyle(cellStyle);
        }

        /*创建主体内容*/
        for (int i = 1;i < content.size();i++) {
            row  = sheet.createRow(i);
            Class<?> aClass = content.get(i).getClass();
            Field[] fields = aClass.getDeclaredFields();
            for (Field field : fields) {
                Column column = field.getAnnotation(Column.class);
                if (column == null) {
                    continue;
                }
                XSSFCell cell = row.createCell(heads.indexOf(column.value()));
//                cell.setCellStyle(cellStyle);
                field.setAccessible(true);

                try {
                    cell.setCellValue(field.get(content.get(i)).toString());
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }

            }

        }
        try {
            workbook.write(new FileOutputStream(filePath));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public void close() {
        if (this.workbook != null) {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private List<String> getHeads() {
        Class tClass = this.content.get(0).getClass();
        Field[] fields = tClass.getDeclaredFields();
        List<String> heads = new ArrayList<>(16);
        for (Field field : fields) {
            Column column = field.getAnnotation(Column.class);
            if (column == null) {
                continue;
            }
            heads.add(column.value());
        }
        return heads;
    }
}
